package taskTracker.ui.lite;

import java.util.Enumeration;
import java.util.Vector;

import javax.microedition.lcdui.Command;

import taskTracker.Project;
import taskTracker.Task;


/**
 * Displays a list of tasks in a project
 * @author aseldawy
 *
 */
public class TaskListUI extends List {

	/**Command for adding new task*/
	final Command cmdAdd = new Command("Add Task", Command.SCREEN, 1);
	/**Command for editing selected task*/
	final Command cmdEdit = new Command("Edit Task", Command.SCREEN, 1);
	/**Command for deleting selected task*/
	final Command cmdDelete = new Command("Delete Task", Command.SCREEN, 1);
	/**Command to issue a delete*/
	final Command cmdBack = new Command("Back", Command.BACK, 1);

	/**The main list that holds tasks*/
	private Vector tasks;
	
	/**The project that holds current tasks*/
	private final Project project;
	
	public TaskListUI(Vector tasks, Project project) {
		super("Tasks", List.IMPLICIT);
		this.project = project;
		this.setTasks(tasks);

		super.addCommand(cmdAdd);
		if (this.size() > 0) {
			super.addCommand(cmdEdit);
			super.addCommand(cmdDelete);
		}
		super.addCommand(cmdBack);
	}

	private void setTasks(Vector tasks) {
		this.deleteAll();
		for (Enumeration e = tasks.elements();e.hasMoreElements();) {
			Task task = (Task) e.nextElement();
			int i = this.append(task, null);
			this.setSelectedIndex(i, task.isCompleted());
		}
		this.tasks = tasks;
	}

	public Vector getTasks() {
		return tasks;
	}

	public Project getProject() {
		return project;
	}

}
